package com.beverages.messages.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beverages.messages.client.CustomerOrdersClient;
import com.beverages.messages.client.MerchantMessagesClient;
import com.beverages.messages.entity.CancelOrderRequest;
import com.beverages.messages.entity.dto.CancelOrderRequestDTO;
import com.beverages.messages.entity.dto.intermediate.OrdersDTO;
import com.beverages.messages.entity.intermediate.CancelOrderReply;
import com.beverages.messages.mapper.CancelOrderRequestMapper;
import com.beverages.messages.service.ICancelOrderRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

@Service
public class CancelOrderRequestServiceImpl extends ServiceImpl<CancelOrderRequestMapper, CancelOrderRequest> implements ICancelOrderRequestService {

    @Autowired
    private CustomerOrdersClient customerOrdersClient;

    @Autowired
    private MerchantMessagesClient merchantMessagesClient;

    @Override
    public CancelOrderRequestDTO convertCancelOrderRequestEntityToCancelOrderRequestTO(CancelOrderRequest cancelOrderRequest){
        CancelOrderRequestDTO dto = new CancelOrderRequestDTO();

        dto.setRequestId(cancelOrderRequest.getId());
        dto.setOrderNumber(cancelOrderRequest.getOrderNumber());
        dto.setRequestTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cancelOrderRequest.getRequestTime()));

        OrdersDTO oneOrderDTO = customerOrdersClient.findOneByOrderNumber(cancelOrderRequest.getOrderNumber()).getData();
        dto.setMilkteaId(oneOrderDTO.getMilkteaId());
        dto.setMilkteaName(oneOrderDTO.getMilkteaName());
        dto.setFromCustomerId(oneOrderDTO.getCustomerId());
        dto.setFromCustomerName(oneOrderDTO.getCustomerName());
        dto.setFromCustomerPhone(oneOrderDTO.getCustomerPhone());

        dto.setRequestContent(cancelOrderRequest.getContent());

        dto.setReplyId(cancelOrderRequest.getReplyId());
        if(! cancelOrderRequest.getReplyId().equals(0)){
            CancelOrderReply cancelOrderReply = merchantMessagesClient.getCancelOrderReplyById(cancelOrderRequest.getReplyId()).getData();
            dto.setReplyContent(cancelOrderReply.getContent());
            dto.setDecision(cancelOrderReply.getDecision());
            dto.setReplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cancelOrderReply.getReplyTime()));
        }

        return dto;
    }
}